<?php

namespace Caisse\CaisseBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
//use Caisse\CaisseBundle\Entity\Article;
use Caisse\CaisseBundle\Entity\Categorie;
use Caisse\CaisseBundle\Form\ArticleType; 
use Caisse\CaisseBundle\Form\CategorieType;
use Symfony\Component\HttpFoundation\Response;

class CategorieController extends Controller
{
    
    public function listCategorieMobileAction()
    {
        $em= $this->getDoctrine()->getManager();
        
        $categories = $em->getRepository("CaisseBundle:Categorie")->findAll();
        $resultat= array();
        foreach ( $categories as $categorie)
        {  
            $tab=array();
           
           // $articles = $em->getRepository("CaisseBundle:Article")->getCategArticle($categorie->getId());
            //foreach ($articles as $article){
              $tab['id']=$categorie->getId();
            $tab['nomCa'] = $categorie->getNomCa();
              //$tab['idA']=$article->getId();
             //$tab['nomArticle'] = $article->getNomArticle();
             //$tab['prix'] = $article->getPrix();
            $resultat['categories'][] = $tab;
         
        
        //}
             
        }
       
        return new Response(json_encode($resultat));
        
    }
    
        public function listCategorieAction()
    {
        $em= $this->getDoctrine()->getManager();
        $categories = $em->getRepository("CaisseBundle:Categorie")->findAll();
        
        return $this->render('CaisseBundle:Categorie:listCategorie.html.twig', array(
            'categories' => $categories,
        ));
    }
 

    
    public function ajoutCategorieAction()
    {   
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        if($id != "")
        $categorie = $em->getRepository('CaisseBundle:Categorie')->find($id);
        else
            $categorie = new Categorie();
        
        $form = $this->createForm(new CategorieType(), $categorie);
        
        
        if ($request->isMethod('POST')) {
            $form->bind($request);
            if ($form->isValid()) {
                $categorie = $form->getData();
                
                
                $repository = $em->getRepository('CaisseBundle:Categorie');
                $em->persist($categorie);
                $em->flush();
                $categories = $repository->findAll();
                return $this->redirect($this->generateUrl("caisse_acceuil"));
            }
        }
        return $this->render('CaisseBundle:Categorie:ajoutCategorie.html.twig', array('form' => $form->createView(), 'id' => $id));

  
        
    }
    public function modifierCategorieAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $categorie = $em->getRepository('CaisseBundle:Categorie')->find($id);
        $form = $this->createForm(new CategorieType(),$categorie);
       // print "xxcxcxc";
        if ($request->isMethod('post')) {
            $categorie->bind($request);
            if ($form->isValid()) {
                $categorie = $form->getData();
                $em->persist($categorie);
                $em->flush();
                $url = $this->generateUrl("caisse_list_categorie");
                return $this->redirect($url);
            }
        }
        return $this->render('CaisseBundle:Article:ajoutCategorie.html.twig', array('form' => $form->createView(), 'id' => $id));

    }
    
    public function supprimerCategorieAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getManager();
        $categorie = $em->getRepository('CaisseBundle:Categorie')->find($id);
        $em->remove($categorie);
        $em->flush();
        $url = $this->generateUrl("caisse_list_categorie");
        return $this->redirect($url);
    }

   }
